interface ServerResponse<T> {
    code: number;
    msg: string;
    data: T
}
Page({
    data: {
        jokeList: [] as string[],
        isRefreshing: false
    },
    onLoad() {
        this.getData()
            .then((res) => {
                this.setData({
                    jokeList: res
                })
            })
    },
    getData() {
        return new Promise<string[]>((resolve, reject) => {
            wx.showLoading({ title: '内容加载中……' })
            wx.request<ServerResponse<string[]>>({
                method: 'GET',
                url: 'https://api-vue-base.itheima.net/api/joke/list',
                data: { num: 10 },
                success: (res) => {
                    resolve(res.data.data)
                },
                fail: () => {
                    wx.showLoading({ title: '请求异常，稍后再试', icon: 'error' })
                    reject()
                },
                complete: () => {

                    wx.hideLoading()


                }
            })
        })


    },
    // 下拉刷新
    async onRefresherrefresh() {
        this.setData({
            isRefreshing: true
        })
        // await this.getData()
        this.setData({
            jokeList:await this.getData()
        })
        this.setData({
            isRefreshing: false
        })
    },
    // 滚动触底
    async getMoreData() {
        // wx.showToast({title:'滚动触底'})
        const res = await this.getData()
        this.setData({
            jokeList: [...this.data.jokeList, ...res]
        })
    },
    goToDetail() {
        wx.navigateTo({
            url: "/pages/hero/list",
        })
    }
})